//
//  pattern-match.swift
//  learnSwitf
//
//  Created by Open on 3/25/15.
//  Copyright (c) 2015 open-groupe. All rights reserved.
//
//http://swifter.tips/pattern-match/

import Cocoa

func demo()
{
    let password = "akfuv(3"
    switch password {
    case "akfuv(3": println("密码通过")
    default:        println("验证失败")
    }
    
    let num: Int? = nil
    switch num {
    case nil: println("没值")
    default:  println("\(num!)")
    }
    
    let x = 0.5
    switch x {
    case -1.0...1.0: println("区间内")
    default: println("区间外")
    }
}

func ~=(pattern: NSRegularExpression, input: String) -> Bool {
    return pattern.numberOfMatchesInString(input,
        options: nil,
        range: NSRange(location: 0, length: countElements(input))) > 0
}

prefix operator ~/ {}

prefix func ~/(pattern: String) -> NSRegularExpression {
    return NSRegularExpression(pattern: pattern, options: nil, error: nil)!
}

func test()
{
    let contact = ("http://onevcat.com", "onev@onevcat.com")
    let mailRegex =
    ~/"^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$"
    let siteRegex =
    ~/"^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$"
    
    switch contact {
    case (siteRegex, mailRegex): println("同时拥有有效的网站和邮箱")
    case (_, mailRegex): println("只拥有有效的邮箱")
    case (siteRegex, _): println("只拥有有效的网站")
    default: println("嘛都没有")
    }

}

